//
//  Album.m
//  05-UI
//
//  Created by Snow on 16/7/20.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "Album.h"
#import "ScrollView.h"

@interface Album ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@end


@implementation Album

- (instancetype)initWithFrame:(CGRect)frame images:(NSArray <UIImage *> *)images
{
    self = [super initWithFrame:frame];
    if (self) {
        images = [self handleImagesWithArray:images];
        [self configWithFrame:frame images:images];
    }
    return self;
}
- (NSArray *)handleImagesWithArray:(NSArray *)array
{
    NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
    UIImage *first = [array firstObject];
    UIImage *last = [array lastObject];
    [arr insertObject:last atIndex:0];
    [arr addObject:first];
    return arr;
}
- (void)configWithFrame:(CGRect)frame images:(NSArray *)array
{
    self.scrollView = [[ScrollView alloc]initWithFrame:frame images:array delegate:self];
    self.scrollView.contentSize = CGSizeMake(frame.size.width * array.count, frame.size.height);
    self.scrollView.contentOffset = CGPointMake(frame.size.width, 0);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    [self addSubview:self.scrollView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView) {
        for (UIScrollView *smallScroll in self.scrollView.subviews) {
            if ([smallScroll.class isSubclassOfClass:[UIScrollView class]]) {
                smallScroll.zoomScale = 1;
            }
        }
    }
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentSize.width - 2 * self.bounds.size.width, 0);
    }
    if (self.scrollView.contentOffset.x == (self.scrollView.contentSize.width - self.bounds.size.width)) {
        self.scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    }
    
}

@end
